package com.weme.matching.view

import android.graphics.Canvas
import android.graphics.Rect
import android.view.View
import androidx.recyclerview.widget.RecyclerView

class OverlapItemDecoration(private val overlapWidth: Int) : RecyclerView.ItemDecoration() {

    override fun getItemOffsets(
        outRect: Rect,
        view: View,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val position = parent.getChildAdapterPosition(view)
        val itemCount = parent.adapter?.itemCount ?: 0

        when {
            itemCount == 1 -> {
                outRect.set(0, 0, 0, 0)
            }
            position == 0 -> {
                outRect.set(0, 0, overlapWidth / 2, 0)
            }
            position == itemCount - 1 -> {
                outRect.set(overlapWidth / 2, 0, 0, 0)
            }
            else -> {
                outRect.set(overlapWidth / 2, 0, overlapWidth / 2, 0)
            }
        }
    }

    override fun onDraw(
        c: Canvas,
        parent: RecyclerView,
        state: RecyclerView.State
    ) {
        val childCount = parent.childCount
        val itemCount = parent.adapter?.itemCount ?: 0

        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val position = parent.getChildAdapterPosition(child)

            if (position == RecyclerView.NO_POSITION) {
                continue
            }

            val left = child.left - overlapWidth / 2
            val right = child.right + overlapWidth / 2
            val top = child.top
            val bottom = child.bottom

            when {
                itemCount == 1 -> {
                    // No overlap for single item
                }
                position == 0 -> {
                    // Overlap right for the first item
                    c.save()
                    c.clipRect(left, top, child.left, bottom)
                    parent.drawChild(c, child, 0)
                    c.restore()
                }
                position == itemCount - 1 -> {
                    // Overlap left for the last item
                    c.save()
                    c.clipRect(child.right, top, right, bottom)
                    parent.drawChild(c, child, 0)
                    c.restore()
                }
                else -> {
                    // Overlap both left and right for other items
                    // ...
                }
            }
        }

        // Draw middle item last
        for (i in 0 until childCount) {
            val child = parent.getChildAt(i)
            val position = parent.getChildAdapterPosition(child)

            if (position == RecyclerView.NO_POSITION) {
                continue
            }

            val left = child.left - overlapWidth / 2
            val right = child.right + overlapWidth / 2
            val top = child.top
            val bottom = child.bottom

            when {
                position != 0 && position != itemCount - 1 -> {
                    // Overlap both left and right for other items
                    c.save()
                    c.clipRect(left, top, child.left, bottom)
                    parent.drawChild(c, child, 0)
                    c.restore()

                    c.save()
                    c.clipRect(child.right, top, right, bottom)
                    parent.drawChild(c, child, 0)
                    c.restore()
                }
            }
        }
    }

}
